package com.qingcheng.controller;

import com.qingcheng.entity.Result;
import com.qingcheng.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常拦截
 */
@ControllerAdvice
public class BaseExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(BaseExceptionHandler.class);

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Result businessException(Exception e) {
        e.printStackTrace();
        logger.info("出现了青橙业务异常：" + e);
        return new Result(501, e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e) {
        e.printStackTrace();
        logger.info("出现了异常" + e);
        return new Result(500, e.getMessage());
    }
}
